Vue3想实例Vuex.Store()却发现不可以使用Vue.use(Vuex) | 您所在的位置:网站首页 › use of undefined type › Vue3想实例Vuex.Store()却发现不可以使用Vue.use(Vuex) |
问题引入
某天你想使用Vuex.Store(),然后就可能遇上如下麻烦 当你用Vue.use(Vuex)就会报如下错误: Uncaught TypeError: Cannot read property ‘use’ of undefined 当你不要Vue.use(Vuex)又会报如下错误: Uncaught Error: [vuex] must call Vue.use(Vuex) before creating a store instance. 习惯了vue2的Vue.use(),在vue3当中就会感到有点蛋疼,因为它更喜欢像createApp()这种导出被暴露的方法去实例化引用。 解决方案: 我们可以通过卸载VueX(3.6)来使它正常工作 npm uninstall vuex 并安装VueX(4.0 alpha) npm i [email protected] main.js import { createApp } from 'vue' import { store } from './store/store' import App from './App.vue' const app = createApp(App) app.use(store) app.mount('#app') store.js import { createStore } from 'vuex' // Create a new store instance. const store = createStore({ state () { return { patients: [] } } }) export default store; 另外我们正在将vuex 3语法与Vue 3一起使用,而vue 3和具有以上语法的vuex 4互相兼容。 |
CopyRight 2018-2019 实验室设备网 版权所有 |